#include "DictProducer.h"
#include "SplitToolCppJieba.h"
#include "Configuration.h"
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>


using std::cout;
using std::endl;

DictProducer::DictProducer(const string& dir)
    :_dir(dir)
     ,_files()
     ,_dict()
     ,_splitTool()
{}
DictProducer::DictProducer(const string& dir,SplitTool *splitTool)
    :_dir(dir)
     ,_files()
     ,_dict()
     ,_splitTool(splitTool)
{}

void DictProducer::build_dict()
{
    for(auto &file:_files){
        read(file.c_str());
    }
}

void DictProducer::build_cn_dict()
{
    for(auto &file:_files){
        read_cn(file.c_str());
    }
}

void DictProducer::store_dict(const char*filepath)
{   
    ofstream ofs(filepath,ios::app);
    if(!ofs){
        perror("ofstream");
        return;
    }
    for(auto &elem:_dict){
        ofs<<elem.first<<" "<<elem.second<<endl;
    }
    ofs.close();
}


void DictProducer::get_files()
{
    getfile(_dir);
}

void DictProducer::getfile(const string&filepath)
{
    DIR*dir=opendir(filepath.c_str());
    if(!dir){
        perror("opendir");
        return;
    }
    struct dirent* pdirent;
    string file;
    while((pdirent=readdir(dir))){
        if(!strcmp(pdirent->d_name,".")||!strcmp(pdirent->d_name,".."))
            continue;
        file=filepath+"/"+string(pdirent->d_name);
        if(pdirent->d_type==4)
            getfile(file);
        else{
            _files.push_back(file);
        }
    }
    closedir(dir);
}
void DictProducer::push_dict(const string &word)
{
    if(word==string())
        return;
    auto it=_dict.insert(make_pair(word,1));
    if(!it.second){
        _dict[word]++;
    }
    /* auto iter=_dict.find(word); */
    /* if(iter==_dict.end()){ */
    /*     _dict.insert(make_pair(word,1)); */
    /* }else{ */
    /*     iter->second++; */
    /* } */
}

void DictProducer::read(const char* filepath)
{
    ifstream ifs(filepath);
    if(!ifs){
        perror("ifstream");
        return;
    }

    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string word;
        while(iss>>word,!iss.eof())
        {
            string newWord=dealword(word);
            push_dict(newWord);
        }
    }
    ifs.close();
}

string DictProducer::dealword(string& word)
{
    for(size_t idx=0;idx!=word.size();idx++)
    {
        if(!isalpha(word[idx]))
            return string();
        else {
            word[idx]=tolower(word[idx]);
        }
    }
    return word;
}

void DictProducer::read_cn(const char* filepath)
{
    ifstream ifs(filepath);
    if(!ifs){
        perror("ifstream");
        return;
    }

    string line;
    while(getline(ifs,line)){
        vector<string> words;
        words=_splitTool->cut(line);
        for(auto &word:words){
            push_dict(word);
        }
    }
    ifs.close();
    
}
void DictProducer::show_files() const
{
    for(auto &path:_files){
        cout<<path<<endl;
    }
}

void DictProducer::show_dict() const
{
    for(auto &elem:_dict)
    {
        cout<<elem.first<<" : "
            <<elem.second<<endl;
        break;
    }
}
/* int main() */
/* { */
/*     Configuration *conf=new Configuration("conf/a.txt"); */
/*     SplitToolCppJieba *tool=new SplitToolCppJieba(*conf); */
/*     DictProducer *dic=new DictProducer("corpora/cn_list",tool); */
/*     dic->get_files(); */
/*     dic->build_cn_dict(); */
/*     dic->store_dict("dictionary/dict.txt"); */
/*     dic->show_files(); */
/*     dic->show_dict(); */

/* } */
